﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using OgilvyOne.BLL;
using OgilvyOne.Model;

public partial class admin_vico_dealer_detail : AdminBasePage
{
    protected string pstr = "";
    protected string cstr = "var citys = Array();\n";
    protected string pid = "";
    protected string cid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);
            DictionaryManageBLL dictbll = new DictionaryManageBLL();

            //省初始
            IList datalist = dictbll.GetDictionary(1);
            for (int i = 0; i < datalist.Count; i++)
            {
                DictionaryManageModel dictmodel = (DictionaryManageModel)datalist[i];
                pstr += "<option value=\"" + dictmodel.Cid + "\">" + dictmodel.Cnname + "</option>\n";
            }
            //市
            DataSet ds = dictbll.GetDictionary(" upid in (select b.CID from CMS_DICTIONARY b where b.upid=1)");
            Hashtable cstable = new Hashtable();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (cstable.ContainsKey(ds.Tables[0].Rows[i]["upid"]))
                {
                    IList tc = ((IList)cstable[ds.Tables[0].Rows[i]["upid"]]);
                    Hashtable tt = new Hashtable();
                    tt["CID"] = ds.Tables[0].Rows[i]["CID"];
                    tt["CNAME"] = ds.Tables[0].Rows[i]["cnname"];
                    tc.Add(tt);
                }
                else
                {
                    IList tc = new ArrayList();
                    Hashtable tt = new Hashtable();
                    tt["CID"] = ds.Tables[0].Rows[i]["CID"];
                    tt["CNAME"] = ds.Tables[0].Rows[i]["cnname"];
                    tc.Add(tt);
                    cstable[ds.Tables[0].Rows[i]["upid"]] = tc;
                }
            }

            //int csi = 0;
            foreach (object obj in cstable.Keys)
            {
                string key = obj.ToString();
                IList ti = (IList)cstable[obj];
                cstr += "citys['" + key + "'] = [";
                for (int i = 0; i < ti.Count; i++)
                {
                    Hashtable tit = (Hashtable)ti[i];
                    cstr += "[" + tit["CID"] + ",'" + tit["CNAME"] + "']";
                    if (i < ti.Count - 1)
                    {
                        cstr += ",";
                    }
                }
                cstr += "]\n";
                //csi++;
            }

            if (!string.IsNullOrEmpty(Request.Params["id"]))
            {//修改
                string _id = Request.Params["id"];
                did.Value = _id;
                VicoDealerModel vdm = new VicoDealerBll().GetModel(int.Parse(_id));
                dealername.Text = vdm.Dealernam;
                dealercode.Text = vdm.Dealercode;
                pid = vdm.Province.ToString();
                cid = vdm.City.ToString();
            }
        }
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        VicoDealerBll vdbll = new VicoDealerBll();
        VicoDealerModel vdm = new VicoDealerModel();
        if (!string.IsNullOrEmpty(did.Value))
        {
            vdm = vdbll.GetModel(int.Parse(did.Value));
        }
        vdm.Dealernam = dealername.Text;
        vdm.Dealercode = dealercode.Text;
        vdm.Ordernum = string.IsNullOrEmpty(ordernum.Value) ? 0 : int.Parse(ordernum.Value);
        vdm.Province = string.IsNullOrEmpty(Request.Params["province"]) ? 0 : int.Parse(Request.Params["province"]);
        vdm.City = string.IsNullOrEmpty(Request.Params["city"]) ? 0 : int.Parse(Request.Params["city"]);
        bool addb = false;
        if (!string.IsNullOrEmpty(did.Value))
        {
            addb = vdbll.Update(vdm);
        }
        else
        {
            vdm.Adduser = GetSessionUsersModel().Username;
            vdm.Addtime = DateTime.Now;
            if (vdbll.Add(vdm) > 0)
            {
                addb = true;
            }
        }
        if (addb)
        {
            Response.Redirect("dealer_list.aspx?layerid=" + Request.Params["layerid"], true);
        }
        Response.Write("<script>alert('保存失败!请重试!')</script>"); 
    }
}
